package com.threegroup.admin.security.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.threegroup.admin.entity.SysRoleEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * springSecurity中的用户实体类
 */
public class SecurityUser implements UserDetails {

    private static final long serialVersionUID = 1L;

    /**
     * user id
     */
    private Long id;

    /**
     * user 用户名
     */
    private String username;

    /**
     * user 密码
     */
    private String password;

    /**
     * 角色
     */
    private String SysRole;

    /**
     * 是否禁用
     */
    private boolean enabled;

    /**
     * 超级管理员   0：否   1：是
     */
    private Integer superAdmin;

    private Long deptId;

    private Collection<? extends GrantedAuthority> authorities;

    /**
     *
     * @param id
     * @param username
     * @param password
     * @param superAdmin
     * @param role
     * @param enabled
     * @param authorities
     */
    public SecurityUser(
            Long id,
            String username,
            String password,
            Integer superAdmin,
            Long deptId,
            SysRoleEntity role, Boolean enabled,
            Collection<? extends GrantedAuthority> authorities) {
        this.id = id;
        this.username = username;
        this.password =password;
        this.superAdmin = superAdmin;
        this.deptId = deptId;
        this.enabled = enabled;
        this.authorities = authorities;
        this.SysRole = role == null ? "普通用户": role.getName();
    }

    /**
     * 无鉴权服务专用SecurityUser
     * @param username
     */
    public SecurityUser(Long id, String username) {
        this.id = id;
        this.username = username;
    }

    /**
     * 返回分配给用户的角色列表
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @JsonIgnore
    public Long getUid() {return id; }

    @Override
    public String getUsername() {
        return username;
    }

    /**
     * 账户是否激活
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * 账户是否未过期
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否未锁定
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否未过期
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    public String getSysRole() {
        return SysRole;
    }

    public Integer getSuperAdmin() {
        return superAdmin;
    }

    public Long getDeptId() {
        return deptId;
    }

    @Override
    public String toString() {
        return "SecurityUser{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", SysRole='" + SysRole + '\'' +
                ", enabled=" + enabled +
                ", authorities=" + authorities +
                '}';
    }
}
